var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:[],
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.getItem();
  },

  formSubmit: function (e) {
    let that = this;
    let last_name = e.detail.value.last_name;
    let company = e.detail.value.company;
    let phone = e.detail.value.phone.trim();
    let province = e.detail.value.province;
    let city = e.detail.value.city;
    let zip = e.detail.value.zip;
    let address = e.detail.value.address;
    var mobile = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    var isMobile = mobile.exec(phone);
    if (!isMobile) {
      wx.showModal({
        title: "提示",
        content: '你输入的电话不符，请重新检查填写'
      })
      return false;
    }
    app.getUserToken(function (err, token) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.globalData.url +'index.php?_m=shop/customers_station/save',
        data: {last_name: last_name, phone: phone,company_name:company, province: province, city: city, zip: zip, address: address, auth_token: token },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.formSubmit();
          } else if (res.data.code == 0) {
            wx.redirectTo({
              url: '../yourUser/yourUser'
            })
          }
        }
      })
    })
  },

  getItem: function (e) {
    let that = this;
    app.getUserToken(function (err, token) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.globalData.url +'index.php?_m=shop/customers_station/get_item',
        data: { auth_token: token },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.formSubmit();
          } else if (res.data.code == 0) {
            console.log(res);
            that.setData({
              info:res.data.data.item
            })
          }
        }
      })
    })
  },

  onShareTap: function (event) {
    wx.showLoading({
      title: '生成中',
    })
    this.setData({
      display: "block",
    })
    var that = this;

    
    that.onshow("智汇GO", "../../images/qy.png");
  
  },

  onshow: function (name, img) {

    var that = this;
    //1. 请求后端API生成小程序码
    //2. canvas绘制文字和图片
    const ctx = wx.createCanvasContext('myCanvas');
    //背景图

    // var bgImgPath = this.data.item.img_url;
    //var bh_bg = "../../images/hb-bg.png";
    ctx.drawImage(img, 0, 0, this.data.windowWidth, 461);

    //var imgPath = that.data.img_qcord;//二维码
    //ctx.drawImage(imgPath, 35, 380, 60, 60);

    //绘制字体
    // ctx.setFontSize(13)
    // ctx.setFillStyle('#6F6F6F')
    // ctx.fillText(name, 10, 338)

    // var td = Math.ceil(this.data.windowWidth / 20);
    // console.log(td);
    // var tr = Math.ceil(this.data.windowHeight / td);

    // for (var i = 0; i < tr; i++) {
    //   var txt = {
    //     x: 140,
    //     y: 310 + (i * 10),
    //     color: '#111111',
    //     size: '13',
    //     align: 'left',
    //     baseline: 'top',
    //     text: this.data.item.name.substring(i * td, 14),
    //     bold: false
    //   };
    //   if (i < 3) {
    //     if (i == 1) {
    //       //txt.text = txt.text
    //       txt.text = txt.text.substring(0, txt.text.length - 3);
    //     }
    //     ctx.save();
    //     ctx.setFontSize(13)
    //     ctx.setFillStyle('#000000')
    //     ctx.setTextAlign('center');
    //     ctx.setTextBaseline('top');
    //     ctx.fillText(txt.text, txt.x, txt.y);
    //   }
    // }
    ctx.draw(false, setTimeout(function () {
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: that.data.windowWidth,
        height: that.data.windowHeight,
        destWidth: that.data.windowWidth * 3,
        destHeight: that.data.windowHeight * 3,
        canvasId: 'myCanvas',
        success: function (res) {
          console.log(res.tempFilePath);
          wx.hideLoading();
          that.setData({
            shareImgSrc: res.tempFilePath
          })

        },
        fail: function (res) {
          wx.hideLoading();
          wx.showToast({
            title: '生成失败',
            icon: "none"
          })
        }
      })
    }, 200));
  },

  showLocal: function () {
    var that = this;
    console.log(that.data.shareImgSrc);
    wx.saveImageToPhotosAlbum({
      filePath: that.data.shareImgSrc,
      success(res) {
        wx.showModal({
          title: '存图成功',
          content: '图片成功保存到相册了，去发圈噻~',
          showCancel: false,
          confirmText: '好哒',
          confirmColor: '#21bba6',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            }

          }
        })
      }, fail: function () {
        wx.showToast({
          title: '保存失败',
          icon: "none"
        })
      }
    })
  },
  hideview: function () {
    this.setData({
      display: "none"
    })
  },

  getQcord() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getQimg',
      data: {
        page: "pages/item/item",
      },
      complete: res => {

        let img = wx.arrayBufferToBase64(res.result.buffer);

        base64src('data:image/jpg;base64,' + img, res => {
          that.setData({
            img_qcord: res
          })
        });
      }
    })
  },

  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }
})